﻿using FBroSharp.Callback;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.VIP;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VIP高级功能测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //创建浏览器
            BrowserDis.CreatBrowser("百度", "www.baidu.com", this.Handle, 0, this.menuStrip1.Height, this.ClientSize.Width, this.ClientSize.Height- this.menuStrip1.Height);
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            for (int i = 0; i < BrowserList.data.Count; i++)
            {
                BrowserList.data[i].MoveWindow(0, this.menuStrip1.Height, this.ClientSize.Width, this.ClientSize.Height- this.menuStrip1.Height, true);

            }
        }
    
        private void 测试地址1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://gongjux.com/fingerprint/");
        }

        private void 测试地址2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://web.uutool.cn/");
        }

        private void 测试地址3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://yisin.gitee.io/ysfile/checkbrowser.html");
        }

        private void 测试地址4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://www.yalala.com/");
        }

        private void 测试地址5ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://browserleaks.com/webrtc");
        }

        private void 测试地址6ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://abrahamjuliot.github.io/creepjs/");
        }

        private void 测试地址7ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://browserleaks.com/webgl");
        }

        private void 测试地址8ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("http://f.vision/");
        }

        private void 指纹刷新VIPToolStripMenuItem_Click(object sender, EventArgs e)
        {

            BrowserList.data[0].ReloadIgnoreCache();
        }

        private void 修改内容ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //全局方式
            //FBroSharpVIPControl.ResponseFilterAddChangeData(FBroSharpResponseURLFindType.ALL, "https://www.baidu.com/",
            //    FBroSharpResponseFilterType.ChangeAllData, "百度", "FBrowserCEF3Lib");

            //按浏览器
            var con = BrowserList.data[0].GetVIPControl();
            con.ResponseFilterAddChangeData(FBroSharpResponseURLFindType.ALL, "https://www.baidu.com/",
                FBroSharpResponseFilterType.ChangeAllData, "百度", "FBrowserCEF3Lib");
            con.ResponseFilterAddChangeData(FBroSharpResponseURLFindType.ALL, "https://www.baidu.com/",
                FBroSharpResponseFilterType.ChangeAllData, "About Baidu", "About FBrowser");
            BrowserList.data[0].ReloadIgnoreCache();
        }

        private void 取消修改内容ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //全局方式
            //FBroSharpVIPControl.ResponseFilterDeleteChangeData("https://www.baidu.com/");

            //按浏览器
            BrowserList.data[0].GetVIPControl().ResponseFilterDeleteChangeData("https://www.baidu.com/");
            BrowserList.data[0].ReloadIgnoreCache();
        }

        private void 用数据替换资源ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            const string soure = @"<html> <head>
                              <meta charset=""UTF-8"">
                              <title>Test Cef</title>
                            </head>
                            <body bgcolor= ""white"">
                            <h2>你好FBrowser！！！</h2>
                            <a style='color:#00ff00'>这是一个直接将百度网页直接替换掉的例子！</a>
                            <div style='color:red'>这里是个div</div>
                            <a href='https://www.baidu.com'>打开百度网页(其实你永远都打不开，哈哈)</a>
                            </body>
                            <script>alert('你好FBrowser')</script></html>";

            //资源协议头
            //FBroSharpStringMap[] header_map = new FBroSharpStringMap[1];
            //header_map[0].key = "content-encoding";
            //header_map[0].value = "gzip";

            //全局方式
            //FBroSharpVIPControl.ResourceHandlerAddChangeData(FBroSharpResponseURLFindType.ALL, "https://www.baidu.com/", "text/html", default/*header_map*/,
            //    System.Text.Encoding.UTF8.GetBytes(soure));

            //按浏览器
            BrowserList.data[0].GetVIPControl().ResourceHandlerAddChangeData(FBroSharpResponseURLFindType.ALL, "https://www.baidu.com/", "text/html", default/*header_map*/,
                System.Text.Encoding.UTF8.GetBytes(soure));
            BrowserList.data[0].ReloadIgnoreCache();
        }

        private void 用本地文件替换资源ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //资源协议头
            FBroSharpStringMap[] header_map = new FBroSharpStringMap[1];
            header_map[0].key = "content-encoding";
            header_map[0].value = "gzip";

            //全局方式
            //FBroSharpVIPControl.ResourceHandlerAddChangeFile(FBroSharpResponseURLFindType.ALL, "https://www.baidu.com/", "application/javascript", header_map,
            //   Directory.GetCurrentDirectory() + "\\..\\..\\html\\t.js");

            //按浏览器
            BrowserList.data[0].GetVIPControl().ResourceHandlerAddChangeFile(FBroSharpResponseURLFindType.ALL, "https://www.baidu.com/", "application/javascript", header_map,
               Directory.GetCurrentDirectory() + "\\..\\..\\html\\t.js");

            BrowserList.data[0].ReloadIgnoreCache();
        }

        private void 取消替换资源ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //全局方式
            //FBroSharpVIPControl.ResourceHandlerDeleteChangeData("https://www.baidu.com/");

            //按浏览器
            BrowserList.data[0].GetVIPControl().ResourceHandlerDeleteChangeData("https://www.baidu.com/");

            BrowserList.data[0].ReloadIgnoreCache();
        }

        private void tLSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://browserleaks.com/tls");
        }

        private void 获取指纹调用数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var control = BrowserList.data[0].GetVIPControl();
            if (control.IsValid)
            {
                Console.WriteLine(control.GetFingerPrintControl().GetFingerCount());
            }
        }

        private void 清空指纹调用数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var control = BrowserList.data[0].GetVIPControl();
            if (control.IsValid)
            {
                var finger = control.GetFingerPrintControl();
                finger.ClearFingerCount();
                Console.WriteLine(finger.GetFingerCount());
            }
        }



        private void webRTCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BrowserList.data[0].GetMainFrame().LoadURL("https://browserleaks.com/webrtc");
        
        }

        private void vIP启用环境ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                advanced.RuntimeEnable(true);
            }
        }

        private void vIP取环境清单IDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                //执行之前需要先RuntimeEnable
                var list = advanced.PageGetContextID();
                for(int i = 0; i < list.GetSize(); i++)
                {
                    Console.WriteLine(list.GetString(i));
                }
            }
        }

        private void vIP执行JSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string contextId = Interaction.InputBox("请输入环境ID，可以通过PageGetContextID获取", "对话框", "", -1, -1);
            if (contextId == "")
                return;

            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                var callback = new VIPJSCallback();
                //执行之前需要先RuntimeEnable
                advanced.RuntimeEvaluate("alert('Hello VIP');function text(a){return a;};text('21313你好')", true, int.Parse(contextId), false, true, 999999, false, false, callback);
  
            }

        }

        private void vIP执行JS框架IDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string frameid = Interaction.InputBox("请输入框架ID，可以通过PageGetContextID获取frameid", "对话框", "", -1, -1);
            if (frameid == "")
                return;

            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                var callback = new VIPJSCallback();
                //执行之前需要先RuntimeEnable
                //   type:     类型：0为指定框架ID,1为主框架,2为全部框架,3为按框架顺序序号
                advanced.RuntimeEvaluate_FrameID("alert('Hello VIP');function text(a){return a;};text('21313你好')",
                    true, 0, 0, frameid, false, true, 999999, false, false, callback);
            }
        }

        private void vIP执行JS主框架ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                var callback = new VIPJSCallback();
                //执行之前需要先RuntimeEnable
                //   type:     类型：0为指定框架ID,1为主框架,2为全部框架,3为按框架顺序序号
                advanced.RuntimeEvaluate_FrameID("alert('Hello VIP');function text(a){return a;};text('21313你好')",
                    true, 1, 0, "", false, true, 999999, false, false, callback);
            }
        }

        private void vIP执行JS全部框架ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                var callback = new VIPJSCallback();
                //执行之前需要先RuntimeEnable
                //   type:     类型：0为指定框架ID,1为主框架,2为全部框架,3为按框架顺序序号
                advanced.RuntimeEvaluate_FrameID("alert('Hello VIP');function text(a){return a;};text('21313你好')",
                    true, 2, 0, "", false, true, 999999, false, false, callback);
            }
        }

        private void vIP执行JS框架序号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string flag = Interaction.InputBox("请输入框架序号，可以通过PageGetContextID获取frameid", "对话框", "", -1, -1);
            if (flag == "")
                return;

            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                var callback = new VIPJSCallback();
                //执行之前需要先RuntimeEnable
                //   type:     类型：0为指定框架ID,1为主框架,2为全部框架,3为按框架顺序序号
                advanced.RuntimeEvaluate_FrameID("alert('Hello VIP');function text(a){return a;};text('21313你好')",
                    true, 3,int.Parse(flag) , "", false, true, 999999, false, false, callback);
            }
        }

        private void vIP关闭环境ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                advanced.RuntimeEnable(false);
            }
        }

        private void vIP截图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();
                var viewport = new FBroSharpViewPort
                {
                    x = 0,
                    y = 0,
                    width = 1024,
                    height = 800,
                    scale = 1
                };

                var callback = new VIPPageCallback();
                advanced.PageCaptureScreenshot("jpeg", 100, viewport, false, true, callback);
            }
        }

        private void vIP触摸事件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();

                //开始
                advanced.DispatchTouchEvent(0, new FBroSharpDevTouchpoint { x = 100, y = 100 }, 0);
                //移动
                advanced.DispatchTouchEvent(1, new FBroSharpDevTouchpoint { x = 60, y = 60 }, 0);
                //结束
                advanced.DispatchTouchEvent(3, new FBroSharpDevTouchpoint { x = 0, y = 0 }, 0);
            }

        }

        private void 触摸测试滑动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();

                advanced.DispatchTouchDown(0, 100);
                advanced.DispatchTouchMove(0, 50);
                advanced.DispatchTouchUp(0, 0);
            }
        }

        private void 触摸测试点击ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();

                advanced.DispatchTouchClick(32, 32, 100);

                //advanced.DispatchTouchDown(32, 32);
                //Thread.Sleep(100);
                //advanced.DispatchTouchUp(32, 32);
            }
        }

        private void 按键测试CAToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();

                advanced.DispatchMouseClick(100, 100, 0, 0);//点击实现焦点
                advanced.DispatchKeyClick((int)Keys.A, 2, 10, false);
            }

        }

        private void 按键测试输入文本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();

                advanced.DispatchKeyInputText("你好abcdefghijklmnopqrstuvwxyz1234567890-=~!@#$%^&*()！", 2);
                advanced.DispatchKeyInputChar("\n\r");
            }
        }

        private void 鼠标测试移动点击ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();

                advanced.DispatchMouseWeel(0, 0, -1000, 0);
                Thread.Sleep(500);
                advanced.DispatchMouseMove(32, 40);
                Thread.Sleep(100);
                advanced.DispatchMouseClick(32, 40, 0, 50);
            }

        }

        private void 鼠标测试滚轮ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var vip_control = BrowserList.data[0].GetVIPControl())
            {
                var advanced = vip_control.GetAdvancedControl();

                advanced.DispatchMouseWeel(0, 0, 1000, 1000);

            }
        }
    }
}
